package com.czh.utils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * jedis 连接池实现
 *
 * @author chenzhihua
 * @Date 2024/5/28
 */
public class JedisConnectUtils {
    // 使用工厂模式
    private static final JedisPool jedisPool;
    private static final String Host = "192.168.187.130";
    private static final Integer PORT = 6379;
    private static final Integer TIMEOUT = 1000;
    private static final String PASSWORD = "123456";

    static {
        // 配置连接池
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        // 最大连接数
        poolConfig.setMaxTotal(8);
        // 最大空闲连接
        poolConfig.setMaxIdle(8);
        // 最小空闲连接
        poolConfig.setMaxIdle(0);
        // 最大等待时间
        poolConfig.setMaxWaitMillis(1000);
        // 创建连接池对象
        jedisPool = new JedisPool(poolConfig, Host, PORT, TIMEOUT, PASSWORD);
    }

    /**
     * 获取jedis对象
     *
     * @return
     */
    public static Jedis getJedis() {
        return jedisPool.getResource();
    }
}
